﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace WRJClient.UI.SystemMonitor
{
    public class MsgBar : UserControl
    {
        private Canvas _canvas;
        private TextBlock _textBlock = null;
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MsgBar), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));

        private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as MsgBar;
            if (control != null)
            {
                control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
            }
        }
        private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            // Remove handler for oldValue.CollectionChanged
            var oldValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
            if (null != oldValueINotifyCollectionChanged)
            {
                oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
            }

            // Add handler for newValue.CollectionChanged (if possible)
            var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
            if (null != newValueINotifyCollectionChanged)
            {
                newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
            }
        }
        void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //Do your stuff here.
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
                {
                    getMsg();
                });
            }

        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MsgBar"/> class.
        /// </summary>
        public MsgBar()
        {
            _canvas = new Canvas();
            _canvas.ClipToBounds = true;
            Content = _canvas;

            _canvas.SetBinding(WidthProperty,
                new Binding { Path = new PropertyPath(ActualWidthProperty), Source = this });
            _canvas.SetBinding(HeightProperty,
                new Binding { Path = new PropertyPath(ActualHeightProperty), Source = this });
        }

        private void getMsg()
        {
            if (_textBlock == null)
            {
                ICollection<string> list = ItemsSource as ICollection<string>;
                if (list != null && list.Count > 0)
                {
                    string msg = list.ElementAt(0);
                    list.Remove(msg);

                    _textBlock = new TextBlock();
                    _textBlock.Text = msg;
                    _textBlock.FontSize = 24;
                    _textBlock.Foreground = Brushes.White;
                    _canvas.Children.Add(_textBlock);
                    _textBlock.UpdateLayout();

                    Canvas.SetLeft(_textBlock, _canvas.ActualWidth);
                    Canvas.SetTop(_textBlock, _canvas.ActualHeight / 2 - _textBlock.ActualHeight / 2);

                    DoubleAnimation animation = new DoubleAnimation();
                    animation.From = _canvas.ActualWidth;
                    animation.To = -_textBlock.ActualWidth;
                    animation.Duration = TimeSpan.FromSeconds(5);
                    animation.Completed += Animation_Completed;
                    _textBlock.BeginAnimation(Canvas.LeftProperty, animation);

                }

            }
        }

        private void Animation_Completed(object sender, EventArgs e)
        {

            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                _canvas.Children.Clear();
                _textBlock = null;
                getMsg();
            });
        }
    }
}